package com.cloudfun.campusshare.repo;

import com.cloudfun.campusshare.entity.UserEntity;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import java.util.Collection;
import java.util.List;

public interface UserEntityRepo extends
        PagingAndSortingRepository<UserEntity, String>,
        JpaSpecificationExecutor<UserEntity> {

    @Query(value = "select distinct password from `user` where address = :address limit 1", nativeQuery = true)
    String findPWDByPubAddress(@Param("address") String address);

    @Query(value = "select * from `user` where phone = :phone", nativeQuery = true)
    UserEntity findbyPhone(@Param("phone") String phone);

    @Query(value = "select username from `user` where id = :id", nativeQuery = true)
    String findUsernameById(@Param("id") String id);

    @Query(value = "select id from `user` where  username like CONCAT('%',:likeName,'%')", nativeQuery = true)
    List<String> findByLikeName(@Param("likeName") String likeName);

    UserEntity findByUsername(String username);

    UserEntity findByEnterpriseName(String enterpriseName);

    List<UserEntity> findByIdIn(Collection<String> ids);

    List<UserEntity> findAllByMerchantId(String merchantId);

    List<UserEntity> findAllByCreatorId(String creatorId);

    UserEntity findByIdAndMerchantId(String id, String merchantId);

    List<UserEntity> findAllByEnable(boolean enable);

    List<UserEntity> findAllByCreatorIdIn(Collection<String> ids);

    @Query(value="select u.id as user_id from user u left join user_role ur on u.id=ur.user_id left join role r on ur.role_id=r.id where r.type='OPERATIONAL_MERCHANT'",nativeQuery=true)
    List<String> findAllMerchant();
}
